<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="年份" prop="year">
        <el-input
          v-model="queryParams.year"
          placeholder="请输入年份"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="项目id" prop="projectId">
        <el-input
          v-model="queryParams.projectId"
          placeholder="请输入项目id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="项目轮数" prop="round">
        <el-input
          v-model="queryParams.round"
          placeholder="请输入项目轮数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="时间段id" prop="timeId">
        <el-input
          v-model="queryParams.timeId"
          placeholder="请输入时间段id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户id" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入用户id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="区域标志" prop="area">
        <el-input
          v-model="queryParams.area"
          placeholder="请输入区域标志"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="满意度" prop="satisfaction">
        <el-input
          v-model="queryParams.satisfaction"
          placeholder="请输入满意度"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="评价" prop="comment">
        <el-input
          v-model="queryParams.comment"
          placeholder="请输入评价"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="填写时间" prop="writeTime">
        <el-date-picker clearable
          v-model="queryParams.writeTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择填写时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['lxy:selectUser:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['lxy:selectUser:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['lxy:selectUser:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['lxy:selectUser:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="selectUserList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" v-if="true"/>
      <el-table-column label="年份" align="center" prop="year" />
      <el-table-column label="项目id" align="center" prop="projectId" />
      <el-table-column label="项目轮数" align="center" prop="round" />
      <el-table-column label="时间段id" align="center" prop="timeId" />
      <el-table-column label="用户id" align="center" prop="userId" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="区域标志" align="center" prop="area" />
      <el-table-column label="满意度" align="center" prop="satisfaction" />
      <el-table-column label="评价" align="center" prop="comment" />
      <el-table-column label="填写时间" align="center" prop="writeTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.writeTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleSatisfactionResult(scope.row)"
            v-hasPermi="['lxy:selectUser:edit']"
          >评价</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['lxy:selectUser:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['lxy:selectUser:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改用户选择对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="时间段id" prop="timeId">
          <el-input v-model="form.timeId" placeholder="请输入时间段id" />
        </el-form-item>
        <el-form-item label="用户id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户id" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSelectUser, getSelectUser, delSelectUser, addSelectUser, updateSelectUser } from "@/api/lxy/selectUser";
import { getTime } from "@/api/lxy/time";
export default {
  name: "SelectUser",
  data() {
    return {
      //当前时间
      nowTime:"",
      //时间段结束时间
      endTime : "",
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户选择表格数据
      selectUserList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        year: undefined,
        projectId: undefined,
        round: undefined,
        timeId: undefined,
        userId: undefined,
        area: undefined,
        satisfaction: undefined,
        comment: undefined,
        writeTime: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验

    };
  },
  created() {
    this.getList();
    // this.getTime();
  },
  mounted() {
    this.getNowTime(); //调用函数
  },
  methods: {
    /** 跳转到评分界面 */
    handleSatisfactionResult: function (row) {
      const endTime=row.endTime;
      const now = this.nowTime;
      if(endTime>now){
        this.$modal.msgError("请在旅行结束后开始评价");
      }
      else {
        this.$router.push({
          path: '/lxy/selectUser/satisfactionForm',
          query: {
            selectUserId: row.id,
            projectId: row.projectId,
            userid: row.userId,
            timeId: row.timeId,
          }
        });
      }
      },
    /** 查询用户选择列表 */
    getList(){
      this.loading = true;
      listSelectUser(this.queryParams).then(response => {
        this.selectUserList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    //查询对应用户的时间段
    getTime(){
      this.loading = true;
      getTime(Number(this.queryParams.timeId)).then(response => {
        this.endTime = response.endTime;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        year: undefined,
        projectId: undefined,
        round: undefined,
        timeId: undefined,
        userId: undefined,
        remark: undefined,
        area: undefined,
        satisfaction: undefined,
        comment: undefined,
        writeTime: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        delFlag: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加用户选择";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      getSelectUser(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改用户选择";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateSelectUser(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addSelectUser(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id
      this.$modal.confirm('是否确认删除用户选择编号为"' + id + '"的数据项？').then(() => {
        this.loading = true;
        return delSelectUser(row.userId,row.timeId);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('lxy/selectUser/export', {
        ...this.queryParams
      }, `selectUser_${new Date().getTime()}.xlsx`)
    }
  },
  getNowTime() {
  var date = new Date();
  var sign2 = ":";
  var year = date.getFullYear() // 年
  var month = date.getMonth() + 1; // 月
  var day = date.getDate(); // 日
  var hour = date.getHours(); // 时
  var minutes = date.getMinutes(); // 分
  var seconds = date.getSeconds() //秒
  var weekArr = ['星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期天'];
  var week = weekArr[date.getDay()];
  // 给一位数的数据前面加 “0”
  if (month >= 1 && month <= 9) {
    month = "0" + month;
  }
  if (day >= 0 && day <= 9) {
    day = "0" + day;
  }
  if (hour >= 0 && hour <= 9) {
    hour = "0" + hour;
  }
  if (minutes >= 0 && minutes <= 9) {
    minutes = "0" + minutes;
  }
  if (seconds >= 0 && seconds <= 9) {
    seconds = "0" + seconds;
  }
  this.nowTime =  year + "-" + month + "-" + day + " " + hour + sign2 + minutes + sign2 + seconds;
  },
};
</script>
